package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import org.omg.CORBA.INTERNAL;

import java.util.*;

/**
 * @author Fengkai
 * @creat 2023/4/21
 * 题目
 * 给定一个乱序的数组
 * 删除所有重复元素
 * 使得每个元素只出现一次
 * 并且按照出现的次数从高到低进行排序
 * 相同出现次数按照第一次出现顺序进行先后排序
 * 数组大小不超过100
 * 数组元素值不超过100
 * 输入
 * 一个数组
 * 输出
 * 去重排序后的数组
 */
public class ArraysCountsSort {
    public static void main(String[] args) {
        String sort  = "1,4,4,4,2,3,3,3,5";
        String[] split = sort.split(",");
        int[] ints = new int[split.length];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = Integer.parseInt(split[i]);
        }
        System.out.println(getArraysCountSort(ints));
    }

    public static String getArraysCountSort(int [] inputs){
        HashMap<Integer, Integer> interCountMap = new LinkedHashMap<>();
        for (int input : inputs) {
            interCountMap.put(input, interCountMap.getOrDefault(input, 0) + 1);
        }
        ArrayList<int[]> ints = new ArrayList<>();
        for (Map.Entry<Integer, Integer> integerIntegerEntry : interCountMap.entrySet()) {
            int[] ints1 = new int[2];
            ints1[0] = integerIntegerEntry.getKey();
            ints1[1] = integerIntegerEntry.getValue();
            ints.add(ints1);
        }
        ints.sort( (v1, v2) -> Integer.compare(v2[1], v1[1]));
        StringBuilder stringBuilder = new StringBuilder();
        for (int[] anInt : ints) {
            stringBuilder.append(anInt[0]).append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }
}
